package com.llkj.contractmanagement.contractmanagement.mine;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.llkj.contractmanagement.main.BaseActivity;
import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.customview.MyDialog;
import com.llkj.contractmanagement.customview.TitleBar;

import butterknife.Bind;

/**
 * author: dong.yj
 * created on: 2016/8/18 12:02
 * description:浏览足迹界面
 */
public class BrowsingHistoryActivity extends BaseActivity implements View.OnClickListener, MyDialog.ItemClickListener {

    @Bind(R.id.title_browinghistory)
    TitleBar titleBar;
    @Bind(R.id.rl_browsinghistory_contract)
    RelativeLayout rl_contract;
    @Bind(R.id.rl_browsinghistory_actor)
    RelativeLayout rl_actor;
    @Bind(R.id.iv_browsing_history_contract)
    ImageView iv_contract;
    @Bind(R.id.iv_browsing_history_actor)
    ImageView iv_actor;
    private BrowsingHistoryActorFragment actorFragment;
    private BrowsingHistoryContractFragment contractFragment;
    private MyDialog myDialog;
    private int index = 0;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_browsinghistory;
    }

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        init();
    }

    private void init() {
        initData();
        setListener();
        setSelection(index);
    }

    private void initData() {

    }

    private void setListener() {
        titleBar.setTopBarClickListener(this);
        rl_actor.setOnClickListener(this);
        rl_contract.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_browsinghistory_contract:
                index = 0;
                setSelection(index);
                break;
            case R.id.rl_browsinghistory_actor:
                index = 1;
                setSelection(index);
                break;
        }
    }

    private void setSelection(int index) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);
        switch (index) {
            case 0:// 合同
                setSelected(rl_contract);
                if (contractFragment == null) {
                    contractFragment = new BrowsingHistoryContractFragment();
                    fragmentTransaction.add(R.id.fl_browsing_history, contractFragment);
                } else {
                    fragmentTransaction.show(contractFragment);
                }
                fragmentTransaction.commit();
                break;
            case 1:// 艺人
                setSelected(rl_actor);
                if (actorFragment == null) {
                    actorFragment = new BrowsingHistoryActorFragment();
                    fragmentTransaction.add(R.id.fl_browsing_history, actorFragment);
                } else {
                    fragmentTransaction.show(actorFragment);
                }
                fragmentTransaction.commit();
                break;

        }

    }

    /**
     * 隐藏所有的fragment
     *
     * @param fragmentTransaction
     */
    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (contractFragment != null) {
            fragmentTransaction.hide(contractFragment);
        }
        if (actorFragment != null) {
            fragmentTransaction.hide(actorFragment);
        }
    }

    private void setSelected(View view) {
        rl_contract.setSelected(false);
        rl_actor.setSelected(false);
        view.setSelected(true);
        if (view.getId() == R.id.rl_browsinghistory_contract) {
            iv_contract.setVisibility(view.VISIBLE);
            iv_actor.setVisibility(view.INVISIBLE);
        } else if (view.getId() == R.id.rl_browsinghistory_actor) {
            iv_contract.setVisibility(view.INVISIBLE);
            iv_actor.setVisibility(view.VISIBLE);
        }
    }

    @Override
    public void rightClick() {
        super.rightClick();
        if (myDialog == null) {
            myDialog = new MyDialog(this, "需要删除所有的浏览记录吗", R.style.MyDialogStyle1);
            myDialog.setLeftText("取消");
            myDialog.setRightText("删除");
            myDialog.setItemClickListener(this);
        }
        myDialog.show();
    }

    @Override
    public void left() {

    }

    @Override
    public void right() {
        if (index == 0) {
            contractFragment.cleanDate();
        }else if (index == 1) {
            actorFragment.cleanDate();
        }

    }
}
